package com.wwj.my_ummary.util.numberUtils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 * Created by admin001 on 2019/8/6
 */
public class StrUtils extends StringUtils {

    /**
     * 字符串空处理，去除首尾空格 如果str为null，返回"",否则返回str
     *
     * @param str
     * @return
     */
    public static String isNull(String str) {
        if (str == null) {
            return "";
        }
        return str.trim();
    }

    /**
     * 判断对象是否为空
     *
     * @param obj
     * @return
     */
    public static boolean isBlank(Object obj) {
        if (obj instanceof String) {
            return isBlank((String) obj);
        } else if (obj == null) {
            return true;
        }
        return false;
    }

    /**
     * String to int
     *
     * @param str
     * @return
     */
    public static int toInt(String str) {
        if (StringUtils.isBlank(str)) {
            return 0;
        }
        int ret = 0;
        try {
            ret = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            ret = 0;
        }
        return ret;
    }

    /**
     * 模版动态参数转换
     *
     * @param content 格式：模版${param}
     * @param param
     * @return
     */
    public static String transTmpl(String content, Map<String, Object> param) {
        //替换参数
        if (param != null) {
            Iterator<Map.Entry<String, Object>> iterator = param.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, Object> entity = iterator.next();
                content = content.replace("${" + entity.getKey() + "}", String.valueOf(entity.getValue()));
            }
        }
        return content;
    }

    /**
     * 隐藏中间n位字符
     *
     * @param string
     * @param n
     * @return
     */
    public static String hideStr(String string, int n) {
        if (n <= 0) {
            return string;
        }
        int length = string.length();
        int index = (length - n) / 2;
        String startStr = string.substring(0, index);
        String endStr = string.substring(index + n, length);
        StringBuffer str = new StringBuffer(startStr);
        for (int i = 0; i < n; i++) {
            str.append("*");
        }
        str.append(endStr);
        return str.toString();
    }

    /**
     * 身份证格式检查
     *
     * @param id
     * @return true正确 false错误
     */
    public static Boolean idNumCheck(String id) {
        String str = "[1-9]{2}[0-9]{4}(19|20)[0-9]{2}"
                + "((0[1-9]{1})|(1[1-2]{1}))((0[1-9]{1})|([1-2]{1}[0-9]{1}|(3[0-1]{1})))"
                + "[0-9]{3}[0-9x]{1}";
        Pattern pattern = Pattern.compile(str);
        Boolean result = pattern.matcher(id).matches() ? true : false;
        return result;
    }

    /**
     * 手机格式验证
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        if (StrUtils.isBlank(phone)) {
            return false;
        }
        String partten = "^(1[1-9][0-9])\\d{8}$";
        return Pattern.matches(partten, phone);
    }

    /**
     * 判断是否是邮箱
     *
     * @param emailAddress
     * @return
     */
    public static Boolean isEmail(String emailAddress) {
        if (StrUtils.isBlank(emailAddress)) {
            return false;
        }
        String pattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        return Pattern.matches(pattern, emailAddress);
    }

    /**
     * 产生唯一 的订单号。
     *
     * @return String
     */
    public static String getOrderNo() {
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        return dateFormat.format(new Date()).substring(2, 8) + String.format("%010d", hashCode);
    }

    /**
     * 生成昵称
     *
     * @return
     */
    public static String getUserName() {
        int hashCode = getHashCode();
        return String.format("%08d", hashCode);
    }

    private static int getHashCode() {
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = -hashCode;
        }
        return hashCode;
    }

    public static String getUUID() {
		/*UUID uuid = UUID.randomUUID();
		String str = uuid.toString();
		// 去掉"-"符号
		String temp = str.substring(0, 8) + str.substring(9, 13)
				+ str.substring(14, 18) + str.substring(19, 23)
				+ str.substring(24);
		return temp;*/

        return UUID.randomUUID().toString().replace("-", "");
    }

    public static boolean imgSuffixCheck(String imgName) {
        if (!imgName.toUpperCase().endsWith("JPG") && !imgName.toUpperCase().endsWith("PNG") && !imgName.toUpperCase().endsWith("JPEG")) {
            return false;
        }
        return true;
    }

    /**
     * 版本号比较
     *
     * @param v1
     * @param v2
     * @return 0代表相等，1代表左边大，-1代表右边大
     * Utils.compareVersion("1.0.358_20180820090554","1.0.358_20180820090553")=1
     */
    public static int compareVersion(String v1, String v2) {
        if (v1.equals(v2)) {
            return 0;
        }
        String[] version1Array = v1.split("[._]");
        String[] version2Array = v2.split("[._]");
        int index = 0;
        int minLen = Math.min(version1Array.length, version2Array.length);
        long diff = 0;

        while (index < minLen
                && (diff = Long.parseLong(version1Array[index])
                - Long.parseLong(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            for (int i = index; i < version1Array.length; i++) {
                if (Long.parseLong(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Long.parseLong(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

    public static void main(String[] args) {

//		System.out.println(imgSuffixCheck("cmc.png"));
//		System.out.println(imgSuffixCheck("cmc.JPG"));
//		System.out.println(imgSuffixCheck("cmc.PNG"));
//		System.out.println(imgSuffixCheck("cmc.yzm"));
    }

}
